import { ipcRenderer } from 'electron';
import _ from 'lodash';

const state = {
    showMenu: true,
    showQuitConfirm: false,
    showOptions: false,
    playing: false,
    locked: false,
    fullScreen: false,
    background_red: 17,
    background_green: 17,
    background_blue: 17,
    show: 5000,
    shuffle: true,
    lockType: 'slide',
    pin: '0000'
};

const mutations = {
    toggleMenu(state) {
        state.showMenu = !state.showMenu;
    },
    hideMenu(state) {
        state.showMenu = false;
    },
    showQuitConfirm(state) {
        state.showMenu = false;
        state.showQuitConfirm = true;
    },
    hideQuitConfirm(state) {
        state.showMenu = true;
        state.showQuitConfirm = false;
    },
    showOptions(state) {
        state.showMenu = false;
        state.showOptions = true;
    },
    showOptions(state) {
        state.showMenu = false;
        state.showOptions = true;
    },
    closeOptions(state) {
        state.showMenu = true;
        state.showOptions = false;
    },
    lockdown(state) {
        state.showMenu = false;
        state.locked = true;
    },
    unlock(state) {
        state.showMenu = true;
        state.locked = false;
    },
    pausePlaying(state) {
        state.playing = false;
    },
    startPlaying(state) {
        state.playing = true;
    },
    setFullScreenFlag(state, payload) {
        state.fullScreen = payload.flag;
    },
    setConfig(state, payload) {
        state.background_red = _.parseInt(payload.background.red);
        state.background_green = _.parseInt(payload.background.green);
        state.background_blue = _.parseInt(payload.background.blue);
        state.show = _.parseInt(payload.show);
        state.shuffle = payload.shuffle;
        state.lockType = payload.lock;
        state.showOptions = false;
        state.showMenu = true;
    },
    setPin(state, payload) {
        state.pin = payload.pin || state.pin;
    }
};

const actions = {
    toggleMenu({ commit, dispatch, rootState }) {
        commit('toggleMenu');
        if (rootState.App.showMenu && rootState.App.playing) {
            dispatch('pause');
        } else {
            if (!_.isNull(rootState.Queue.selectedDirectory)) dispatch('pendingImageSelect');
        }
    },
    hideMenu({ commit }) {
        commit('hideMenu');
    },
    pendingQuit({ commit }) {
        commit('showQuitConfirm');
    },
    cancelQuit({ commit }) {
        commit('hideQuitConfirm');
    },
    confirmQuit({ commit }) {
        ipcRenderer.send('quit-app');
    },
    showOptions({ commit }) {
        commit('showOptions');
    },
    closeOptions({ commit }) {
        commit('closeOptions');
    },
    lockdown({ commit, dispatch }) {
        dispatch('hideImage');
        dispatch('pause');
        commit('lockdown');
    },
    unlock({ commit, dispatch, rootState }) {
        if (!_.isNull(rootState.Queue.selectedDirectory)) dispatch('pendingImageSelect');
        commit('unlock');
        if (!_.isNull(rootState.Queue.selectedDirectory)) dispatch('hideMenu');
    },
    pause({ commit }) {
        commit('pausePlaying');
    },
    play({ commit }) {
        commit('startPlaying');
    },
    enterFullScreen({ commit }) {
        commit('setFullScreenFlag', { flag: true });
    },
    leaveFullScreen({ commit }) {
        commit('setFullScreenFlag', { flag: false });
    },
    setConfig({ commit }, payload) {
        commit('setConfig', payload);
        commit('setPin', payload);
    },
    saveConfig({ dispatch }, payload) {
        dispatch('setConfig', payload);
        ipcRenderer.send('save-config', payload);
    },
    saveNewPin({ commit }, payload) {
        commit('setPin', payload);
        ipcRenderer.send('save-new-pin', payload);
    }
};

export default {
    state,
    mutations,
    actions
};
